import Vue from 'vue'
import Vuex from 'vuex'
import service from "../service";

Vue.use(Vuex)

export default new Vuex.Store({
  namespaced:true,
  state:Object.assign({
      clientData:{},
      clientCount:0,
      leadData:{},
      leadCount:0,
      loginStatus:false,
      opInfo:{},
      userData:{},
      seaData:{},
      seaCount:0,
      priceData:{},
      priceCount:0,
      clientDropData:{},
      clientDropCount:0,
      leadDropData:{},
      leadDropCount:0,
      myleadData:{},
      myleadCounr:0
  }),
  mutations:{
      updateclientdata(state,payload){
          state.clientData = {...payload};
      },
      updateclientcount(state,payload){
          state.clientCount = new Number(payload)
      },
      updateclientdropdata(state,payload){
          state.clientDropData = {...payload};
      },
      updateclientdropcount(state,payload){
          state.clientDropCount = new Number(payload)
      },
      updateleaddropdata(state,payload){
          state.leadDropData = {...payload};
      },
      updateleaddropcount(state,payload){
          state.leadDropCount = new Number(payload)
      },
      updateleaddata(state,payload){
          state.leadData = {...payload};
      },
      updatemyleaddata(state,payload){
          state.myleadData = {...payload};
      },
      updateleadcount(state,payload){
          state.leadCount = new Number(payload)
      },
      updatemyleadcount(state,payload){
          state.myleadCount = new Number(payload)
      },
      updateLogStatus(state,payload){
          state.loginStatus = payload
      },
      updateOpInfo(state,payload){
          state.opInfo = payload
      },
      updateuserdata(state,payload){
          state.userData = payload;
      },
      updateseadata(state,payload){
          state.seaData = payload;
      },
      updateseacount(state,payload){
          state.seaCount = new Number(payload)
      },
      updateprice(state,payload) {
          state.priceData = payload;
      },
      updatepricecount(state,payload) {
          state.priceCount = payload;
      },
  },
  actions:{
      getClients({commit},payload){
          var page = payload?payload:1;
          service.instance().get("http://"+window.location.hostname+":8011/Client/getClientsAll/"+page)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有查看客户列表的权限，可联系管理员添加', {icon: 2});
                  }
                  else {
                      commit('updateclientdata', response.data);
                  }
              })
              .catch((error)=> {
              });
      },
      getClientsCounts({commit}){
          service.instance().get("http://"+window.location.hostname+":8011/Client/getClientsCount")
              .then((response)=> {
                  commit('updateclientcount',response.data)
              }).catch((error)=> {
          })},
      searchClient({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/Client/search/"+payload).then((response)=> {
              if(response.data === "NOPERMISSION"){
                  layer.msg('您没有查看客户列表的权限，可联系管理员添加', {icon: 2});
              }
              else {
                  commit('updateclientdata', response.data);
              }
          }).catch((error)=> {
              });
      },

      deleteClient({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/Client/deleteClient/"+payload)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有删除客户的权限，当前操作未成功', {icon: 2});
                  }
                  else{
                      layer.msg('已删除该用户', {icon: 1});
                  }
              })
              .catch((error)=> {
              });
      },
      movetosea({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/Client/moveToSea/"+payload)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有移动客户的权限，当前操作未成功', {icon: 2});
                  }
                  else{
                      layer.msg('已移至公海', {icon: 1});
                  }
              })
              .catch((error)=> {
              });
      },
      getleads({commit},payload){
          var page = payload?payload:1;
          service.instance().get("http://"+window.location.hostname+":8011/lead/getleadsAll/"+page)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有查看线索列表的权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      commit('updateleaddata',response.data);
                  }
              })
              .catch((error)=> {
              });
      },
      getmyleads({commit},payload){
          var page = payload?payload:1;
          service.instance().get("http://"+window.location.hostname+":8011/lead/getmyleadsAll/"+page)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有查看线索列表的权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      commit('updatemyleaddata',response.data);
                  }
              })
              .catch((error)=> {
              });
      },
      getleadsCounts({commit}){
          service.instance().get("http://"+window.location.hostname+":8011/lead/getleadsCount")
              .then((response)=> {
                  commit('updateleadcount',response.data)
              })
              .catch((error)=> {
              });
      },

      getmyleadsCounts({commit}){
          service.instance().get("http://"+window.location.hostname+":8011/lead/getmyleadsCounts")
              .then((response)=> {
                  commit('updatemyleadcount',response.data)
              })
              .catch((error)=> {s
              });
      },
      deletelead({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/lead/deletelead/"+payload)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有删除线索列表的权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      layer.msg('已删除该线索', {icon: 1});
                  }
              })
              .catch((error)=> {
              });
      },
      updateLoginStatus({commit},payload){
          commit('updateLogStatus',payload)
      },
      updateOPInfo({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/getOpInfo")
              .then((response)=> {
                  commit('updateOpInfo',response.data)
              })
              .catch((error)=> {
              });
      },
      oplogout(){
          service.instance().get("http://"+window.location.hostname+":8011/logout")
              .then((response)=> {
                  sessionStorage.removeItem("isin");
                  setTimeout(window.location.reload(),1000)
              })
              .catch((error)=> {
              });
      },
      getUser({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/user/getUserAll")
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有查看系统用户的权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      commit('updateuserdata',response.data);
                  }
              })
              .catch((error)=> {
              });
      },

      getSea({commit},payload){
          var page = payload?payload:1;
          service.instance().get("http://"+window.location.hostname+":8011/Client/getSea/"+page)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有查看公海的权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      commit('updateseadata',response.data);
                  }
              })
              .catch((error)=> {
              });
      },
      getSeaCount({commit}){
          service.instance().get("http://"+window.location.hostname+":8011/Client/getSeaCount")
              .then((response)=> {
                  commit('updateseacount',response.data)
              })
              .catch((error)=> {
              });
      },
      getprice({commit},payload){
          var page = payload?payload:1;
          service.instance().get("http://"+window.location.hostname+":8011/price/getPriceAll/"+page)
              .then((response)=> {
                  commit('updateprice',response.data)
              })
              .catch((error)=> {
              });
      },
      getpriceCounts({commit}){
          service.instance().get("http://"+window.location.hostname+":8011/price/getcount")
              .then((response)=> {
                  commit('updatepricecount',response.data)
              })
              .catch((error)=> {
              });
      },
      deletePrice({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/price/delete/"+payload)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有删除售价表权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      layer.msg('已删除该售价信息', {icon: 1});
                  }
              })
              .catch((error)=> {
              });
      },
      getClientsDrop({commit},payload){
          var page = payload?payload:1;
          service.instance().get("http://"+window.location.hostname+":8011/Client/getClientsDropAll/"+page)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有查看客户作废区的权限，可联系管理员添加', {icon: 2});
                  }
                  else {
                      commit('updateclientdropdata', response.data);
                  }
              })
              .catch((error)=> {
              });
      },
      getClientsDropCounts({commit}){
          service.instance().get("http://"+window.location.hostname+":8011/Client/getClientsDropCount")
              .then((response)=> {
                  commit('updateclientdropcount',response.data)
              }).catch((error)=> {
          })},
      getdropleads({commit},payload){
          var page = payload?payload:1;
          service.instance().get("http://"+window.location.hostname+":8011/lead/getLeadsDropAll/"+page)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有查看线索作废区的权限，可联系管理员添加', {icon: 2});
                  }
                  else {
                      commit('updateleaddropdata', response.data);
                  }
              })
              .catch((error)=> {
              });
      },
      getleadsDropCounts({commit}){
          service.instance().get("http://"+window.location.hostname+":8011/lead/getLeadsDropCount")
              .then((response)=> {
                  commit('updateleaddropcount',response.data)
              }).catch((error)=> {
          })},
      recoverylead({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/lead/recover/"+payload)
              .then((response)=> {

              }).catch((error)=> {
          })
      },
      recoveryclient({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/Client/recover/"+payload)
              .then((response)=> {

              }).catch((error)=> {
          })
      },
      droplead({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/lead/drop/"+payload)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有作废权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      layer.msg("已作废（管理员可在作废区还原）")
                  }
              }).catch((error)=> {
          })
      },
      dropclient({commit},payload){
          service.instance().get("http://"+window.location.hostname+":8011/Client/drop/"+payload)
              .then((response)=> {
                  if(response.data === "NOPERMISSION"){
                      layer.msg('您没有作废权限，可联系管理员添加', {icon: 2});
                  }
                  else{
                      layer.msg("已作废（管理员可在作废区还原）")
                  }
              }).catch((error)=> {
          })
      },
  }
})
